
#include "StdAfx.h"
#include "DBString.h"
#include "DBTypeTable.h"
#include <Tokenizer.h>

DBString::DBString(){ 
	buf_ = ""; 
}

DBString::~DBString(){
}

void DBString::arrange_str( char* org, char* target, int target_size ){
	if(! org )
		return;

	if(! target )
		return;

	if(! target_size )
		return;

	int org_size = strlen( org );

	if( org_size >= target_size )
		return;

	for( int i = 0 ; i <= org_size ; i++ ){
		if( org[ i ] == '\'' )
			target[ i ] = ' ';
		else if( org[ i ] == '\\' )
			target[ i ] = ' ';
		else if( org[ i ] == '"' )
			target[ i ] = ' ';		
		else
			target[ i ] = org[ i ];
	}
}

bool DBString::createTable( DBTypeTable* type_table, DBTableDescription* table_desc, int dbms_type, StringObj& str ){
	int type = 0;
	int size = 0;

	char* type_name = 0;
	char* column_name = 0;
	char* table_name = (char*)table_desc->getName();

	IS_NULL_RETURN( table_name, false );
	IS_LEN_ZERO_RETURN( table_name, false );

	str = "CREATE TABLE ";
	str += table_name;
	str += "( ";

	size_t field_count = table_desc->getColumnCount();
	IS_NULL_RETURN( field_count, false );

	for( size_t i = 0 ; i < field_count ; i++ ){	
		type = table_desc->getColumnType(i);
		size = table_desc->getColumnBufferSize( i );

		column_name = table_desc->getColumnName( i );
		IS_NULL_RETURN( column_name, false );
		IS_LEN_ZERO_RETURN( column_name, false );

		str += column_name;

		if( dbms_type == DBTypeTable::DEF_SQL_SERVER )
			type_name = type_table->getSQLServer_Type( type );		
		else if( dbms_type == DBTypeTable::DEF_MY_SQL )
			type_name = type_table->getSQLServer_Type( type );		
		else if( dbms_type == DBTypeTable::DEF_ORACLE )
			type_name = type_table->getOracle_Type( type );		
		else if( dbms_type == DBTypeTable::DEF_POSTGRES ){
			type_name = type_table->getOracle_Type( type );
		}

		IS_NULL_RETURN( type_name, 0 );

		if( ( type == DBTypeTable::DEF_TYPE_CHAR ) || \
			( type == DBTypeTable::DEF_TYPE_UCHAR ) )
		{
			str += " ";
			str += type_name;
			str += "(";
			str += ( size + 1 );
			str += ")";
		}
		else{
			str += " ";
			str += type_name;
		}	

		if( i != ( field_count - 1 ) )
			str += ",";
	}

	str += ");";

	return true;
}

bool DBString::insertValue( DBTableDescription* table_desc, StringObj& str ){	
	char* table_name = (char*)table_desc->getName();
	char* first = table_desc->getColumnName(0);
	
	IS_NULL_RETURN( table_name, false );
	IS_LEN_ZERO_RETURN( table_name, false );

	IS_NULL_RETURN( first, false );
	IS_LEN_ZERO_RETURN( first, false );	

	int targetFieldCount = table_desc->getColumnCount();
	IS_NULL_RETURN( targetFieldCount, false );

	str = "INSERT INTO ";
	str += table_name;
	str += " (";
	
	int i = 0;
	for( i = 0 ; i < targetFieldCount ; i++ ){
		str += table_desc->getColumnName(i);

		if( i != ( targetFieldCount - 1 ) )
			str += ",";
	}
	
	str +=") VALUES (";
	for( i = 0 ; i < targetFieldCount ; i++ ){
		str += "?";

		if( i != ( targetFieldCount - 1 ) )
			str += ",";
	}

	str += ");";
	
	return true;
}

void DBString::set_sql( char* sql ){
	if(! sql )
		return;

	buf_.clear();
	buf_ = sql;
}

void DBString::set_sql_arg( char* arg, bool next ){
	if(! buf_.size() )
		return;

	if(! arg )
		return;

	buf_ += "'";
	buf_ += arg;
	buf_ += "'";
	
	if( next )
		buf_ += ",";
}

void DBString::set_sql_arg( int arg, bool next ){
	if(! buf_.size() )
		return;

	buf_ += arg;

	if( next )
		buf_ += ",";
}

void DBString::set_sql_arg( int* data, size_t size, bool next ){
	if(! buf_.size() )
		return;

	for( size_t i = 0 ; i < size ; i++ ){
		buf_ += data[ i ];
		buf_ += ",";
	}

	if( next )
		buf_ += ",";
}

void DBString::set_sql_arg( __int64* data, size_t size, bool next ){
	if(! buf_.size() )
		return;

	for( size_t i = 0 ; i < size ; i++ ){
		buf_ += data[ i ];

		if( i < ( size - 1 ) )
			buf_ += ",";
		else{
			if( next )
				buf_ += ",";
		}
	}
}

void DBString::set_sql_done( char* sql ){
	if(! buf_.size() )
		return;

	buf_ += sql;
}
